<?php
namespace My\Controller;

use Utuo\BackgroundController;

class DistributionShopSourceController extends BackgroundController {
	
	protected function _initialize() {
		$this->setControllerTitle('渠道管理');
		$this->setActionTitle(array(
			'index' => '渠道列表',
	        'add' => '添加渠道',
			'modify' => '修改渠道',
			'url' => '推广链接',
			'user' => '渠道用户'
		));
		$this->setActionNav(array(
			'index',
			'add'
		));
	}
	
	/**
	 * 红包列表
	 */
	public function getLists() {
		$page = $this->getPage ();
		$pagesize = $this->getPageSize ();
		$d = M("DistributionShopSource");
		$condition = array (
				'cid' => $this->aid,
		);
		$sourceLists = $d->where ( $condition )->limit ( $pagesize )->page ( $page )->order ( 'id DESC' )->select ();
		$sourceCount = $d->where ( $condition )->count ();
		$this->assign ( 'sourceLists', $sourceLists );
		$this->showPage ( $sourceCount, $pagesize );
		$this->display();
	}
	
	/**
	 * 推广链接
	 */
	public function urlAction() {
		$id = I('id', 0, "intval");
		$d = D("DistributionShopSource");
		$source = $d->where("cid=%d and id=%d", $this->aid, $id)->find();
		$source and $source['sourceLink'] = array(
				'homeUrl' => U('Home/DisShop/index', "sid={$this->aid}&qid={$source['id']}", false, true),
				'tryUrl' => U('Home/DisShopTry/show', "sid={$this->aid}&qid={$source['id']}", false, true),
		);
		$this->assign("source", $source);
		$this->display();
	}
	
	/**
	 * 渠道的用户列表
	 */
	public function userAction() {
		$page = $this->getPage ();
		$pagesize = $this->getPageSize();
		$id = I("id", 0, "intval");
		$userLists = M("WechatUser")->where("cid=%d and qid=%d", $this->aid, $id)->limit ( $pagesize )->page ( $page )->order ( 'id DESC' )->select();
		$sourceCount = M("WechatUser")->where ( "cid=%d and qid=%d", $this->aid, $id )->count ();
		$this->assign("userLists", $userLists);
		$this->showPage ( $sourceCount, $pagesize );
		$this->display();
	}
	
	public function orderAction() {
		
	}
	
}